<?php

/**
 * Database Helper class
 *
 */

class DatabaseHelper {
	private $mysql_connected = false;
	
	function __construct() {
		@mysql_connect( 'localhost', 'root', '' );
		if ( @mysql_select_db( 'hk_rent' ) ) {
			$this->mysql_connected = true;
			//Set charset to UTF-8
			mysql_set_charset( 'utf8' );
		}
		//Also set locale in database helper
		setLocale( LC_ALL, 'et_EE' );
		date_default_timezone_set( 'Europe/Tallinn' );
	}
	
	/**
	 * Get is mysql connected
	 *
	 * @return boolean true | false
	 */
	
	function isConnected() {
		return $this->mysql_connected;
	}
	
	/**
	 * Get mysql errno
	 *
	 * @return int errno
	 */
	
	function hasError() {
		return mysql_errno(); //Uses last opened mysql connection
	}
	
	/**
	 * Get last error as text
	 *
	 * @return String error
	 */
	
	function getError() {
		if ( $this->hasError() ) {
			return mysql_error(); //Uses last opened mysql connection
		}
		return false;
	}
	
	/**
	 * This method does the actual query to the database
	 *
	 * @return boolean true on success
	 */
	
	function query( $query ) {
		if ( $this->isConnected() ) {
			$result = mysql_query( $query );
			//echo $query;
			if ( $this->hasError() ) {
				echo $this->getError();
			}
			return $result;
		}
		return false;
	}
	
	/**
	 * Escape query string
	 *
	 * @return escaped query string
	 */
	
	public static function escape( $query_param ) {
		return mysql_real_escape_string( trim( strip_tags( $query_param ) ) );
	}
}


?>